import { firstToUpper } from './index';
import React from 'react';
import type { MenuDataItem } from '@ant-design/pro-layout';
import * as allIcons from '@ant-design/icons';

// 处理菜单Icon
export const fixMenuItemIcons = (menus: MenuDataItem[], iconType = 'Outlined'): MenuDataItem[] => {
  menus.forEach((menu) => {
    const { icon, children } = menu;
    if (typeof icon === 'string') {
      const fixIcon = firstToUpper(icon) + iconType;
      menu.icon = React.createElement(allIcons[fixIcon] || allIcons[icon]);
    }
    if (children && children.length > 0) {
      menu.children = fixMenuItemIcons(children);
    }
    menu.access = 'canAdmin';
    delete menu.component;
  });
  return menus;
};

export const fixMenuItemIcon = (icon: string, iconType = 'Outlined') => {
  if (icon) {
    const fixIcon = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType;
    return React.createElement(allIcons[fixIcon] || allIcons[icon]);
  }
  return '';
};
